package com.example.administrator.kaohedemo.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.kaohedemo.Action.Actions;
import com.example.administrator.kaohedemo.Action.CourseListAction;
import com.example.administrator.kaohedemo.Model.CourseListBean;
import com.example.administrator.kaohedemo.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/22.
 */
public class TypePopViewAdapter extends RecyclerView.Adapter<TypePopViewAdapter.MyViewHolder> {

    private final CourseListAction courseListAction;
    private Context context;
    private List<CourseListBean.Mbody.Type> list=new ArrayList<CourseListBean.Mbody.Type>();
    private Map<String ,String> map=new HashMap<String ,String>();
    private String key="";

    public TypePopViewAdapter(Context context){
        courseListAction=CourseListAction.getInstense();
        this.context=context;
    }

    public void setList(List<CourseListBean.Mbody.Type> mDatas){
        list.clear();
        list.addAll(mDatas);
        notifyDataSetChanged();
    }

    public void setMapKey(String key){
        this.key=key;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_resource_type,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.tv_subject_version_name.setText(list.get(position).name);
        if (TextUtils.isEmpty(map.get(key))||!map.get(key).equals(list.get(position).id)){
            holder.iv_subject_version_select.setImageResource(R.mipmap.feed_back_nor);
        }else {
            holder.iv_subject_version_select.setImageResource(R.mipmap.feed_back_sel);
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv_subject_version_name;
        ImageView iv_subject_version_select;

        public MyViewHolder(View itemView) {
            super(itemView);

            tv_subject_version_name= (TextView) itemView.findViewById(R.id.tv_subject_version_name);
            iv_subject_version_select= (ImageView) itemView.findViewById(R.id.iv_subject_version_select);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    map.put(key,list.get(getLayoutPosition()).id);
                    courseListAction.SendPopClick(Actions.CourseListActions.TYPE_POP_ONCLICK,list.get(getLayoutPosition()).id);
                }
            });
        }
    }
}
